package br.com.viewcar.web.bean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.faces.model.SelectItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import br.com.viewcar.entity.Marca;
import br.com.viewcar.service.MarcaService;

@Scope("session")
@Component("marcaBean")
public class MarcaBean extends EntityBean<Integer, Marca> {

	@Autowired
	private MarcaService service;

	protected Integer retrieveEntityId(Marca entity) {

		return entity.getId();
	}

	protected MarcaService retrieveEntityService() {

		return this.service;
	}

	protected Marca createNewEntity() {

		return new Marca();
	}

	public List<SelectItem> getListaMarcas() {

		List<SelectItem> result = new ArrayList<SelectItem>();

		List<Marca> marcas = service.retrieveAll();

		Collections.sort(marcas, new Comparator<Marca>() {

			public int compare(Marca o1, Marca o2) {

				return o1.getDescricao().compareTo(o2.getDescricao());

			}});

		for (Marca marca : marcas) {

			result.add(new SelectItem(marca.getId(), marca.getDescricao()));
		}

		return result;
	}


}
